home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / term / pm.trm < prev    next >
Text File  |  1993-05-11  |  7KB  |  286 lines

  1. /*
  2.  *    pm.trm  --- inboard terminal driver for Presentation Manager
  3.  *            --- after X-11 driver, by R.W.Fearick 31/1/92.
  4.  *    v1.1 11/8/92 -- speed things up        
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <process.h>
  9.  
  10. /* 
  11.    include all stuff from os2.h as GNUPLOT uses INT as an enum and
  12.    this clashes with #defines in os2.h 
  13. */
  14.  
  15. typedef unsigned short USHORT;
  16. typedef USHORT *PUSHORT;
  17. typedef void *PVOID ;
  18. typedef char *PCHAR ;
  19.  
  20. typedef long LONG;
  21. typedef LONG *PLONG;
  22.  
  23. typedef unsigned long ULONG;
  24. typedef ULONG *PULONG;
  25. typedef struct
  26. {
  27.   ULONG  tib2_ultid;
  28.   ULONG  tib2_ulpri;
  29.   ULONG  tib2_version;
  30.   USHORT tib2_usMCCount;
  31.   USHORT tib2_fMCForceFlag;
  32. } TIB2;
  33. typedef TIB2 *PTIB2;
  34.  
  35. typedef struct
  36. {
  37.   PVOID tib_pexchain;
  38.   PVOID tib_pstack;
  39.   PVOID tib_pstacklimit;
  40.   PTIB2 tib_ptib2;
  41.   ULONG tib_version;
  42.   ULONG tib_ordinal;
  43. } TIB;
  44. typedef TIB *PTIB;
  45.  
  46. typedef struct
  47. {
  48.   ULONG pib_ulpid;
  49.   ULONG pib_ulppid;
  50.   ULONG pib_hmte;
  51.   PCHAR pib_pchcmd;
  52.   PCHAR pib_pchenv;
  53.   ULONG pib_flstatus;
  54.   ULONG pib_ultype;
  55. } PIB;
  56. typedef PIB *PPIB;
  57. typedef ULONG HEV;
  58. typedef HEV *PHEV;
  59.  
  60. ULONG DosCreateEventSem (const char *, PHEV, ULONG, ULONG);
  61. ULONG DosWaitEventSem (HEV, ULONG);
  62. ULONG DosGetInfoBlocks (PTIB *, PPIB *);
  63. ULONG DosSearchPath( ULONG, char*, char*, char*, ULONG ) ;
  64.  
  65. /* define PM world coordinate limits */
  66. #define PM_XMAX 4096
  67. #define PM_YMAX 4096
  68.  
  69. /* approximations for typical font/screen sizes */
  70.  
  71. #define PM_VCHAR (PM_YMAX/30) 
  72. #define PM_HCHAR (PM_XMAX/80) 
  73. #define PM_VTIC (PM_YMAX/100)
  74. #define PM_HTIC (PM_XMAX/150)
  75.  
  76. /* graphics commands */
  77. #define SET_GRAPHICS    'G'
  78. #define SET_TEXT        'E'
  79. #define SET_LINE        'L'
  80. #define SET_ANGLE       'A'
  81. #define SET_JUSTIFY     'J'
  82. #define SET_POINTMODE   'D'
  83. #define GR_MOVE         'M'
  84. #define GR_DRAW         'V'
  85. #define GR_RESET        'R'
  86. #define GR_TEXT         'T'
  87. #define GR_PAUSE        'P'
  88. #define GR_HELP         'H'
  89. #define PM_nopts 1
  90.  
  91. static char PM_path[256] = "" ;  /* path for pm program */
  92. static int  PM_mode      = 0 ;   /* track mode to avoid redraw after hitting break */
  93. static     HEV hev ;
  94.  
  95. char PM_opts[PM_nopts][20] = {
  96.    " "
  97.    };
  98. int PM_optarg[PM_nopts] = { 
  99.    0
  100.    };
  101.  
  102. FILE *PM_pipe=NULL, *fopen();
  103. char PM_command[1024]= "gnuplot_PM -name gnuplot";
  104.  
  105.  
  106. /*   PM_args - scan gnuplot command line for options */
  107.  
  108. PM_args(argc, argv) int argc; char *argv[]; {
  109.    int nPM = 0, n;
  110.    if( PM_path[0]=='\0' ) getcwd( PM_path, 256 ) ;
  111.    return(nPM);
  112.    }
  113.  
  114. PM_init() 
  115.     { 
  116.     static char buffer[1024] ;
  117.     int pid ;
  118.     int rc ;
  119.     PPIB pib ;
  120.     PTIB tib ;
  121.     char semname[32] ;
  122.     char pipename[32] ;
  123.     char tempname[32] ;
  124.     if( PM_pipe == NULL ) {
  125.         strcpy( tempname, "gpXXXXXX" ) ;
  126.         if( mktemp( tempname ) == NULL ) {
  127.             fprintf( stderr, "Temp name failure !\n" ) ;
  128.             abort() ;   
  129.             }
  130.         strcpy( semname, "\\sem32\\" ) ;
  131.         strcpy( pipename, "\\pipe\\" ) ;
  132.         strcat( semname, tempname ) ;
  133.         strcat( pipename, tempname ) ;
  134.         strcat( PM_path, "\\gnupmdrv.exe" ) ;
  135.         rc = access( PM_path, 0 ) ;
  136.             /* find exe file */ 
  137.   
  138.         if( rc != 0 ) 
  139.             rc = DosSearchPath( 0x0002, /* search GNUPLOT environment */
  140.                                 "GNUPLOT",
  141.                                 "gnupmdrv.exe",
  142.                                 PM_path,
  143.                                 256 ) ; 
  144.  
  145.         if( rc != 0 ) 
  146.             rc = DosSearchPath( 0x0003,  /* then try current directory & path */
  147.                                 "PATH",
  148.                                 "gnupmdrv.exe",
  149.                                 PM_path,
  150.                                 256 ) ; 
  151.         if( rc != 0 ) {
  152.             fprintf( stderr, "Can't find gnupmdrv.exe !\n" ) ;
  153.             abort() ;   
  154.             }
  155.                             
  156.         rc = DosCreateEventSem( semname, &hev, 1, 0 ) ;
  157.         if( rc != 0 ) {
  158.             fprintf( stderr, "Can't create semaphore !\n" ) ;
  159.             abort() ;   
  160.             }
  161.         pid=spawnl( P_SESSION|P_DEFAULT, PM_path, "GnuplotPM", tempname, NULL ) ;
  162.         if( rc == -1 ) {
  163.             fprintf( stderr, "Can't spawn gnupmdrv.exe !\n" ) ;
  164.             abort() ;   
  165.             }
  166.  
  167.         DosGetInfoBlocks( &tib, &pib ) ;
  168.         DosWaitEventSem( hev, 10000 ) ;        
  169.         PM_pipe = fopen( pipename, "r+b" ) ; 
  170.         if( PM_pipe == NULL ) {
  171.             fprintf( stderr, "Can't open pipe to gnupmdrv.exe !\n" ) ;
  172.             abort() ;   
  173.             }
  174.         setvbuf( PM_pipe, buffer, _IOFBF, 1024 ) ;
  175.         pid = pib->pib_ulpid ;
  176.         fwrite( &pid, 1, 4, PM_pipe ) ;
  177.         fflush( PM_pipe ) ;
  178.         }
  179.     }
  180.  
  181. PM_reset() {
  182.         putc( GR_RESET, PM_pipe); 
  183.         fflush(PM_pipe);
  184.         }
  185.  
  186. PM_text() 
  187.     {
  188.     if( PM_mode != SET_TEXT ) { 
  189.         putc( SET_TEXT, PM_pipe); 
  190.         fflush(PM_pipe);
  191.         }
  192.     PM_mode = SET_TEXT ;
  193.     }
  194.  
  195. PM_graphics() 
  196.     { 
  197.     putc( SET_GRAPHICS, PM_pipe); 
  198.     PM_mode = SET_GRAPHICS ;
  199.     }
  200.  
  201. PM_move(unsigned int x, unsigned int y) 
  202.     { 
  203.     putc( GR_MOVE, PM_pipe ) ;
  204.     fwrite( &x, sizeof(int), 1, PM_pipe ) ;
  205.     fwrite( &y, sizeof(int), 1, PM_pipe ) ;
  206.     }
  207.  
  208. PM_vector(unsigned int x, unsigned int y)
  209.     { 
  210.     putc( GR_DRAW, PM_pipe ) ;
  211.     fwrite( &x, sizeof(int), 1, PM_pipe ) ;
  212.     fwrite( &y, sizeof(int), 1, PM_pipe ) ;
  213.     }
  214.  
  215. PM_linetype(int lt)
  216.     { 
  217.     putc( SET_LINE, PM_pipe ) ;
  218.     fwrite( <, sizeof(int), 1, PM_pipe ) ;
  219.     }
  220.  
  221. PM_text_angle( int ta)
  222.     { 
  223.     putc( SET_ANGLE, PM_pipe ) ;
  224.     fwrite( &ta, sizeof(int), 1, PM_pipe ) ;
  225.     return(TRUE) ; 
  226.     }
  227.  
  228. PM_put_text(unsigned int x, unsigned int y, char *str) 
  229.     {
  230.     int len ;
  231.     putc( GR_TEXT, PM_pipe ) ;
  232.     fwrite( &x, sizeof(int), 1, PM_pipe ) ;
  233.     fwrite( &y, sizeof(int), 1, PM_pipe ) ;
  234.     len = strlen( str ) + 1 ;
  235.     fwrite( &len, sizeof(int), 1, PM_pipe ) ;
  236.     fwrite( str, 1, len, PM_pipe ) ;
  237.     for( len=sizeof(int)-len%sizeof(int); len > 0 ; len-- )  /* pad rest of int with zeros */
  238.         putc( '\0', PM_pipe ) ;
  239.     }
  240.  
  241. PM_justify_text( enum JUSTIFY mode ) 
  242.     {
  243.     putc( SET_JUSTIFY, PM_pipe ) ;
  244.     fwrite( &mode, sizeof(int), 1, PM_pipe ) ;
  245.     return(TRUE);
  246.     }
  247.  
  248. PM_point( int x, int y, int number )
  249. /*
  250. ** tell the driver we are plotting a point so it can decide whether to
  251. ** use colour or not
  252. */
  253.     {
  254.     int mode ;
  255.     mode=1 ;
  256.     putc( SET_POINTMODE, PM_pipe ) ;
  257.     fwrite( &mode, sizeof(int), 1, PM_pipe ) ;
  258.     do_point( x, y, number ) ;
  259.     mode = 0 ;
  260.     putc( SET_POINTMODE, PM_pipe ) ;
  261.     fwrite( &mode, sizeof(int), 1, PM_pipe ) ;
  262.     }
  263.  
  264. PM_pause( char *str )
  265. /*
  266. ** pause - using message box on PM screen
  267. */
  268.     {
  269.     int len, cbR, rc ;
  270.     unsigned long ul ;
  271.     char buf[256] ;
  272.     char *bp ;
  273.  
  274.     if( PM_pipe == NULL ) return 2 ;
  275.     bp=buf ;
  276.     putc( GR_PAUSE, PM_pipe ) ;
  277.     len = strlen( str ) + 1 ;
  278.     fwrite( &len, sizeof(int), 1, PM_pipe ) ;
  279.     fwrite( str, 1, len, PM_pipe ) ;
  280.     for( rc=sizeof(int)-len%sizeof(int); rc > 0 ; rc-- )  /* pad rest of int with zeros */
  281.         putc( '\0', PM_pipe ) ;
  282.     fflush(PM_pipe ) ;
  283.     rc=DosRead( fileno(PM_pipe), &len, sizeof(int), &cbR ) ;
  284.     return len ;
  285.     }
  286.